/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author        Notes
 * 2021-10-10     Sherman       first version
 * 2021-11-03     Sherman       Add icu_sample
 */

#include <rtthread.h>
#include "hal_data.h"
#include <rtdevice.h>
#include <stdio.h>

#define LED3_PIN    BSP_IO_PORT_01_PIN_06
#define USER_INPUT  "P105"

int status = PIN_LOW;


/*********************************************************************
 * @fn      led_set
 *
 * @brief   use pins driver to set gpio
 *
 * @return  none
 */
int led_set(int index, int value)
{
    rt_base_t pin = index==3 ? LED3_PIN : 0;
    rt_base_t val = value == 1 ? PIN_HIGH : PIN_LOW;
    status = val;
    rt_pin_mode(pin, PIN_MODE_OUTPUT);
    rt_pin_write(pin, val);
    return 0;
}

void hal_entry(void)
{
    rt_kprintf("\nHello RT-Thread!\n");

    led_set(3, status);
    while (1)
    {
        //rt_pin_write(LED3_PIN, PIN_HIGH);
        //rt_thread_mdelay(500);
        //rt_pin_write(LED3_PIN, PIN_LOW);
        rt_thread_mdelay(500);
    }
}

void irq_callback_test(void *args)
{
    rt_kprintf("\n IRQ00 triggered \n");
    status = status==PIN_HIGH ? PIN_LOW : PIN_HIGH;
    led_set(3, status);
}

void icu_sample(void)
{
    /* init */
    rt_uint32_t pin = rt_pin_get(USER_INPUT);
    rt_kprintf("\n pin number : 0x%04X \n", pin);
    rt_err_t err = rt_pin_attach_irq(pin, PIN_IRQ_MODE_RISING, irq_callback_test, RT_NULL);
    if(RT_EOK != err)
    {
        rt_kprintf("\n attach irq failed. \n");
    }
    err = rt_pin_irq_enable(pin, PIN_IRQ_ENABLE);
    if(RT_EOK != err)
    {
        rt_kprintf("\n enable irq failed. \n");
    }
}

static void test(int argc, char**argv)
{
    if (argc < 2)
    {
        rt_kprintf("Please input'test param'\n");
        return;
    }

    for(int i=1;i<argc;i++) {
        rt_kprintf("param%d is %s\n", i, argv[i]);
    }

    if (rt_strcmp(argv[1], "led")==0)
    {
        rt_kprintf("run LED command!\n");
        //led();
    }
    if (rt_strcmp(argv[1], "led_set")==0 && argc==4)
    {
        int index = 0;
        int value = 0;
        sscanf(argv[2], "%d", &index);
        sscanf(argv[3], "%d", &value);

        rt_kprintf("led_set %d %d\n", index, value);
        led_set(index, value);
    }
}

#define DBG_BUFF_MAX_LEN          256

/* debug print : support float double */
int dbg_printf(const char *fmt, ...)
{
    va_list args;
    static char rt_log_buf[DBG_BUFF_MAX_LEN] = { 0 };

    va_start(args, fmt);
    int length = vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);

    rt_kputs(rt_log_buf);

    return length;
}

static void millis_test()
{
    for(int i=0;i<10;i++) {
        int64_t ticks = rt_tick_get();
        float t = 1.0 / RT_TICK_PER_SECOND * ticks;
        dbg_printf("RT_TICK_PER_SECOND=%d ticks=%d millis=%f%d\n", RT_TICK_PER_SECOND, ticks, t);
        rt_thread_mdelay(123);
    }
}

MSH_CMD_EXPORT(millis_test, test command: test param);
MSH_CMD_EXPORT(test, test command: test param);
MSH_CMD_EXPORT(icu_sample, icu sample);
